package com.english_test_sys.service.Impl;

import com.english_test_sys.mapper.RankingMapper;
import com.english_test_sys.pojo.UserRanking;
import com.english_test_sys.service.RankingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RankingServiceImpl implements RankingService {

    @Autowired
    private RankingMapper rankingMapper;

    @Override
    public Map<String, Object> getRankingData(String type) {
        Map<String, Object> result = new HashMap<>();

        // 参数标准化处理
        String queryType = validateTypeParam(type);

        // 获取排行榜数据（同步查询条件）
        List<UserRanking> rankingList = rankingMapper.getRankingList(queryType);
        result.put("users", rankingList);

        // 获取总用户数（同步查询条件）
        Integer totalUsers = rankingMapper.getTotalUsers(queryType);
        result.put("totalUsers", totalUsers != null ? totalUsers : 0);

        return result;
    }

    private String validateTypeParam(String type) {
        // 处理空参数和无效参数
        if (type == null || type.trim().isEmpty()) {
            return "all";
        }

        // 标准化参数值
        return switch (type.toLowerCase()) {
            case "student", "admin" -> type.toLowerCase();
            default -> "all";
        };
    }
}